#!/bin/bash
if [ "${1}" == "-h" -o "$1" == "--help" ]
then
echo "Copyright (c) 2019-2021 The Spark Project"
echo "本工具可以快速打包依赖于spark-webapp-runtime（在非UOS/deepin上是swrt-lite）web app应用"
echo
echo "This tool can pack web apps quickly with spark-webapp-runtime(swrt-lite on other distros instead of UOS/deepin)"
echo
echo 用法
echo web-packer 链接 标题 图标路径 软件描述 软件包名 维护者 版本号
echo 如果没有任何参数，将以交互式模式运行
echo
echo Usage:
echo web-packer url title icon_path desciption pkgname maintainer version
echo If there are no parameters, it will run in interactive mode 
exit 0
fi

if [ "${1}" == "-v" -o "${1}" == "--version" ];then
echo "Copyright (c) 2019-2021 The Spark Project"
echo 
echo "0.1.2"
exit 0
fi



#reset
echo "Copyright (c) 2019-2021 The Spark Project"
echo 本工具可以快速打包依赖于spark-webapp-runtime（在非UOS上是swrt-lite）web app应用
echo 本工具只能打包在线网页或本地网页，离线服务器网页等高级操作参考以下链接
echo
echo https://gitee.com/deepin-community-store/spark-web-app-runtime/

cd /tmp
mkdir swrt-app-maker
cd swrt-app-maker
#工作目录应当为/tmp/swrt-maker

while [ ! $url ]
do
if [ ! $1 ]
    then 
    echo "没有检测任何参数，以交互式模式运行"
    echo "请输入你要打包的网址(需要填写协议，比如https://spark-app.store的https://也是需要的)"
    read url
else
    echo "检测到url，跳过获取"
    url="$1"
    echo "读取到的url为：$url"
fi

if [ ! $url ]
	then
	echo "没有检测到网址，请重新输入！"
	sleep 3
	clear
else 
	echo "网址读取成功，检测是否为http/file开头"
fi

urlstring=${url}
urlstr=${urlstring:0:4}
if [ "http" == ${urlstr} -o "file" == ${urlstr} ]
then
echo "检测到http/https/file协议，继续"
echo 
else
echo "没有检测到http/https/file类型协议，请确认你填写了"
sleep 3
unset url
clear
fi

done

while [ ! $title ]
do
if [ ! $2 ]
    then 
    echo "请输入你要打包的应用标题"
    echo
    read title
else
    echo "检测到标题，跳过获取"
    title="$2"
    echo "读取到的标题为：$title"
fi

done

while [ ! $icon ]
do
if [ ! $3 ]
    then 
    echo "请输入你要打包的应用图标绝对路径，可以拖动图标文件到终端，只支持png"
    read icon
    echo 
else
    echo "检测到图标，跳过获取"
    icon="$3"
    echo "读取到的图标路径为：$icon"
fi
iconpath=`echo "$icon" | sed $'s/\'//g'`
icon=$iconpath
echo "去除可能的单引号后得到：$icon"
echo 


#'

done

while [ ! $des ]
do
if [ ! $4 ]
    then 
    echo "请输入你要打包的应用描述"
    read des
    echo 
else
    echo "检测到描述，跳过获取"
    des="$4"
    echo "读取到的描述为：$des"
fi

done

while [ ! $pkgname ]
do
if [ ! $5 ]
    then 
    echo "请输入你要打包的应用包名（一般为web-xxxxxx，比如web-baidu.com，不能有中文或特殊符号或空格，空格用-代替）"
    read pkgname
    echo 
else
    echo "检测到包名，跳过获取"
    pkgname="$5"
    echo "读取到的包名为：$pkgname"
fi

done

while [ ! $maintainer ]
do
if [ ! $6 ]
    then 
    echo "请输入你的昵称（这会出现在deb中）"
    read maintainer
    echo 
else
    echo "检测到维护者名，跳过获取"
    maintainer="$6"
    echo "读取到的维护者为：$maintainer"
fi

done

while [ ! $version ]
do
if [ ! "$7" -a ! "$6" ]
    then 
    echo "请输入文件版本号，这不是必须的。如果你想要更新一个已有的应用那么请输入比1.0大的版本"
    echo "如果不是，请直接输入回车，会默认填写1.0"
    read version
    echo 
    if [ ! $version ];then
    version="1.0"
    fi
    
elif [ ! $7 -a $6 ];then
echo "在参数模式，但未检测到版本号"
echo "默认版本号为1.0"
version="1.0"

else 
    echo "检测到版本号，跳过获取"
    version="$7"
    echo "读取到的版本号为：$version"
fi

done


echo "开始生成deb包"
echo "输出的control文件为"
echo ""
echo "Package: $pkgname"
echo "Version: $version"
echo "Depends: spark-webapp-runtime(>1.5)"
echo "Maintainer: $maintainer"
echo "Description: A webapp of $url packed by $maintainer using spark webapp runtime"
echo "Architecture: amd64"
echo "-----------------------开始生成-----------------------"
mkdir DEBIAN
cd DEBIAN
touch control
echo "Package: $pkgname" >> ./control 
echo "Version: $version" >> ./control 
echo "Depends: spark-webapp-runtime(>=1.5)"  >> ./control 
echo "Maintainer: $maintainer" >> ./control 
echo "Description: A webapp of $url packed by $maintainer using spark webapp runtime" >> ./control 
echo "Architecture: amd64" >> ./control
cd ..
echo  "-----------------------开始生成desktop-----------------------"
mkdir -p usr/share/applications
cd usr/share/applications
touch $pkgname.desktop
echo [Desktop Entry] >> $pkgname.desktop
echo Name=$title >> $pkgname.desktop
echo Exec=spark-webapp-runtime -u "$url" -i "/opt/durapps/$pkgname/icon.png" -t "$title" -d "$des" >> $pkgname.desktop
echo Type=Application  >> $pkgname.desktop
echo Terminal=false >> $pkgname.desktop
echo StartupNotify=true >> $pkgname.desktop
echo Encoding=UTF-8 >> $pkgname.desktop
echo Comment=$title >> $pkgname.desktop
echo Icon=/opt/durapps/$pkgname/icon.png >> $pkgname.desktop

cat $pkgname.desktop

echo "-----------------------开始复制icon-----------------------"
cd ../../../
mkdir -p opt/durapps/$pkgname
cd opt/durapps/$pkgname
cp $icon ./icon.png
cd ../../
echo "-----------------------开始打包-----------------------"
cd ..
workdir=`pwd`
echo "回退到工作目录，当前目录位于$workdir"
fakeroot dpkg -b . ../
mv ../${pkgname}_${version}_amd64.deb ~/${pkgname}_${version}_amd64.deb
echo "-----------------------打包结束，软件包在您的用户目录-----------------------"
rm -rf /tmp/swrt-app-maker
xdg-open ~ &
sleep 5